package com.xqboss.apps.listener;

import com.xqboss.apps.constant.RedisKey;
import com.xqboss.apps.enums.box.BoxSequenceEnum;
import com.xqboss.apps.enums.mq.AppMqDelayEnum;
import com.xqboss.apps.service.marketing.AirdropRecordService;
import com.xqboss.apps.service.marketing.AirdropService;
import com.xqboss.apps.service.msg.PushService;
import com.xqboss.common.core.mq.MqEnum;
import com.xqboss.common.core.mq.MqListener;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.utils.StringUtils;
import org.apache.rocketmq.client.apis.consumer.ConsumeResult;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author Trent
 * @date 2023/9/20
 */

@Component
public class AirdropListener extends MqListener {

    @Lazy
    @Autowired
    private AirdropService airdropService;

    @Autowired
    private RedisCache redisCache;

    @Override
    public MqEnum getEnum() {
        return AppMqDelayEnum.AIRDROP_START;
    }

    @Override
    public ConsumeResult solve(MessageView message) {
        String value = StandardCharsets.UTF_8.decode(message.getBody()).toString();
        long id = Long.parseLong(value);//空投记录id
        Object cacheObject = redisCache.getCacheObject(RedisKey.KEY_AIRDROP_ID + id);
        int sequence = cacheObject==null?BoxSequenceEnum.RANDOM.getValue():Integer.parseInt(cacheObject.toString());
        boolean isRandom = (sequence == BoxSequenceEnum.RANDOM.getValue());//true 随机空投（藏品|装备）
        airdropService.dispatchAirdrop(id,isRandom);
        return ConsumeResult.SUCCESS;
    }
}